
def composer(f, g):
    """Return the composition function which given x, computes f(g(x))."""
    return lambda x: f(g(x))

def composite_identity(f, g):
    """
    Return a function with one parameter x that returns True if f(g(x)) is
    equal to g(f(x)). You can assume the result of g(x) is a valid input for f
    and vice versa.
    """
    return lambda x: f(g(x)) == g(f(x))


def sum_digits(y):
    total = 0
    while y > 0:
        total, y = total + y % 10, y // 10
    return total

def is_prime(n):
    if n == 1:
        return False
    k = 2
    while k < n:
        if n % k == 0:
            return False
        k += 1
    return True

def count_cond(condition):
    """Returns a function with one parameter N that counts all the numbers from
    1 to N that satisfy the two-argument predicate function Condition, where
    the first argument for Condition is N and the second argument is the
    number from 1 to N.
    """
    def f(N):
        cnt = 0
        for i in range(1, N + 1):
            cnt += condition(N, i)
        return cnt
    return f


def multiple(a, b):
    """Return the smallest number n that is a multiple of both a and b."""
    def gcd(a, b):
        return a if b == 0 else gcd(b, a % b)
    return a * b // gcd(a, b)
    


def cycle(f1, f2, f3):
    """Returns a function that is itself a higher-order function..."""
    def g(n):
        def h(x):
            for i in range(1, n + 1):
                if i % 3 == 1: x = f1(x)
                elif i % 3 == 2: x = f2(x)
                else: x = f3(x)
            return x
        return h
    return g

